package com.cykj.enterprise.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.alipay.api.internal.util.AlipaySignature;
import com.cykj.common.dto.ResponseDTO;
import com.cykj.common.entity.OrderDO;
import com.cykj.enterprise.mapper.PayMapper;
import com.cykj.enterprise.service.IHrService;
import com.cykj.enterprise.service.IPayService;
import com.cykj.enterprise.template.AlipayTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @BelongsProject: czjyProgram3
 * @BelongsPackage: per.czjy.controller
 * @Author: 蔡张君逸
 * @CreateTime: 2025-08-20 16:39
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("vip")
public class VipController {
    @Resource
    AlipayTemplate alipayTemplate;
    @Autowired
    IPayService payService;
    @Autowired
    ObjectMapper objectMapper;

    public static final ConcurrentMap<String,HttpSession> paySessionMap=new ConcurrentHashMap<>();
    @Autowired
    private PayMapper payMapper;

    //   充值，沙箱支付
    @SaCheckLogin
    @GetMapping(value = "doPay", produces = "text/html")
    public String pay(@RequestParam("num") int num, @RequestParam("duration")int duration, HttpSession httpSession) {
        int hrId = StpUtil.getLoginIdAsInt();
        return payService.pay(hrId,num,duration);
    }

    @PostMapping("notify")  // 注意这里必须是POST接口
    public String payNotify(HttpServletRequest request,HttpSession httpSession) throws Exception {
        if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
            System.out.println("=========支付宝异步回调========");

            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = request.getParameterMap();
            for (String name : requestParams.keySet()) {
                params.put(name, request.getParameter(name));
//                System.out.println(name + " = " + request.getParameter(name));
            }
            // 支付宝验签，确定是支付宝发送的订单信息，避免伪造
            //此处是公钥验签
            if(AlipaySignature.rsaCheckV1(params,alipayTemplate.getAlipayPublicKey(),alipayTemplate.getCharset(),alipayTemplate.getSignType())){
                // 验签通过
                System.out.println("交易名称: " + params.get("subject"));
                System.out.println("交易状态: " + params.get("trade_status"));
                System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
                System.out.println("商户订单号: " + params.get("out_trade_no"));
                System.out.println("交易金额: " + params.get("total_amount"));
                System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
                System.out.println("买家付款时间: " + params.get("gmt_payment"));
                System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));


                String s = params.get("passback_params");

                OrderDO orderDO = objectMapper.readValue(s, OrderDO.class);
                int res=payMapper.addOrder(orderDO);
                ResponseDTO responseDTO=payService.updateVipTime(orderDO.getRelativeId(),orderDO.getBuyDuration());


            }
            else {
                System.out.println("验签未通过");
            }
        }
        return "success";
    }

    @GetMapping("getVipInfo")
    public ResponseDTO getVipInfo(@RequestParam("companyId") String companyId){
        String hrId = StpUtil.getLoginIdAsString();
        return payService.getVipInfo(hrId);
    }

    @GetMapping("getVipGoods")
    public ResponseDTO getVipGoods(){
        return payService.getVipGoods();
    }

}